#import "InfoControllerBase.h"

@interface InfoControllerBase ()
@property (nonatomic) BOOL isFirstTime;
@property (weak, nonatomic) IBOutlet UILabel *nomeLabel;
@property (weak, nonatomic) IBOutlet UITextView *telefoneTextView;
@property (weak, nonatomic) IBOutlet UITextView *faxTextView;
@property (weak, nonatomic) IBOutlet UITextView *celularTextView;
@property (weak, nonatomic) IBOutlet UITextView *emailTextView;
@property (weak, nonatomic) IBOutlet UILabel *ruaLabel;
@property (weak, nonatomic) IBOutlet UILabel *numeroLabel;
@property (weak, nonatomic) IBOutlet UILabel *complementoLabel;
@property (weak, nonatomic) IBOutlet UILabel *bairroLabel;
@property (weak, nonatomic) IBOutlet UILabel *cepLabel;
@property (weak, nonatomic) IBOutlet UILabel *cidadeLabel;
@property (weak, nonatomic) IBOutlet UILabel *estadoLabel;
@end

@implementation InfoControllerBase

@synthesize isFirstTime;
@synthesize model;
@synthesize repositoryProvider;
@synthesize nomeLabel;
@synthesize telefoneTextView;
@synthesize faxTextView;
@synthesize celularTextView;
@synthesize emailTextView;
@synthesize ruaLabel;
@synthesize numeroLabel;
@synthesize complementoLabel;
@synthesize bairroLabel;
@synthesize cepLabel;
@synthesize cidadeLabel;
@synthesize estadoLabel;

-(void)viewDidLoad {
    [super viewDidLoad];
    self.isFirstTime = YES;
}

-(InfoBase*)getInfo:(NSError**)error {
    return [[self.repositoryProvider getRepositoryForClass:[self.model class]] getModelInfoById:self.model.id error:error];
}

-(void)setLabels:(InfoBase*)info {
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    if (!self.isFirstTime) {
        return;
    }
    
    self.isFirstTime = NO;
    UIBarButtonItem* rightItem = self.navigationItem.rightBarButtonItem;
    UIActivityIndicatorView* spinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    [spinner startAnimating];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:spinner];
    
    dispatch_queue_t queue = dispatch_queue_create("Load Info", NULL);
    dispatch_async(queue, ^(){
        NSError* error;
        InfoBase* info = [self getInfo:&error];
        dispatch_async(dispatch_get_main_queue(), ^(){
            [spinner stopAnimating];
            self.navigationItem.rightBarButtonItem = rightItem;
            if (error) {
                [[[UIAlertView alloc] initWithTitle:@"Erro" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
            } else {
                self.nomeLabel.text = self.model.text;
                self.bairroLabel.text = info.bairro;
                self.cepLabel.text = info.cep;
                self.celularTextView.text = info.celular;
                self.cidadeLabel.text = info.cidade;
                self.complementoLabel.text = info.complemento;
                self.emailTextView.text = info.email;
                self.estadoLabel.text = info.estado;
                self.faxTextView.text = info.fax;
                self.numeroLabel.text = info.numero;
                self.ruaLabel.text = info.rua;
                self.telefoneTextView.text = info.telefone;
                [self setLabels:info];
            }
        });
    });
}

@end
